package com.shujia.controller;

import com.shujia.entity.Dept;
import com.shujia.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
//@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("all")
    public String getAllDepts(Model model){

        List<Dept> allDeptInfo = deptService.getAllDeptInfo();
        model.addAttribute("depts",allDeptInfo);

//        return allDeptInfo.toString();
        return "showall";
    }

    @RequestMapping("selectOne")
    public String selectOneDept(int deptno){
        Dept deptByNo = deptService.getDeptByNo(deptno);
        return deptByNo.toString();
    }

    @RequestMapping("save")
    public String saveOneDept(int deptno,String dname,String loc){
        Dept dept = new Dept(deptno, dname, loc);
        String s = deptService.saveDept(dept);
        //return i==1?"数据保存成功！！":"数据保存失败！！";
        if("数据保存成功！！".equals(s)){
            return "status";
        }else {
            return "fail";
        }
    }

}
